package com.egao.orderB.service.impl;


import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.StringUtil;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.AddressBiz;
import com.egao.orderB.biz.AddressFactoryBiz;
import com.egao.orderB.biz.MaintainFeeBiz;
import com.egao.orderB.entity.Address;
import com.egao.orderB.entity.AddressFactory;
import com.egao.orderB.entity.AddressTemp;
import com.egao.orderB.entity.MaintainFee;
import com.egao.orderB.service.AddressService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//import com.egao.orderB.mapper.AddressMapper;
//import javax.transaction.Transactional;

/**
 * 地址报价服务实现类
 * @author : Doman
 * @date : 2020-06-22 10:37:54
 * @version ：1.0
 */
@Service
public class AddressServiceImpl /*extends ServiceImpl<AddressMapper, Address> */implements AddressService {

	@Autowired
	private AddressBiz addressBiz;
	@Autowired
	private AddressFactoryBiz addressFactoryBiz;
	@Autowired
	private MaintainFeeBiz maintainFeeBiz;

    /**
    @Override
    public PageResult<Address> listPage(PageParam<Address> page) {
        List<Address> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<Address> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, Address address, User loginUser)  throws Exception{
        try {
            JsonResult jr = addressBiz.list(page,address,loginUser);
            List<Address> addressList = (List<Address>)jr.getData();
            if(addressList != null && addressList.size() > 0) {
                for(int i = 0; i < addressList.size(); i ++) {
                    Address a2 = addressList.get(i);
                    AddressFactory af = new AddressFactory();
                    af.setAddressId(a2.getId());
                    a2.setAddressFactoryList(addressFactoryBiz.list(af, loginUser));
                }
            }
            return jr;
        } catch (Exception e) {
            throw new Exception("查询错误,请联系平台管理员!");
        }
    }

    @Override
    public JsonResult list(Address address, User loginUser) {
//        List<Address> list = addressBiz.list(address,loginUser);
//        for(Address f: list) {
//            if(f.getPinyin()==null || f.getPinyin().equals(""))
//            addressBiz.update(f,loginUser);
//        }
        return JsonResult.ok().setData(addressBiz.list(address,loginUser));
    }

    @Override
    public JsonResult searchAddressFee(Address address, User loginUser) {
        if(Utils.isEmpty(address.getDock())){
            return JsonResult.error("请选择码头!");
        }
        List<Address> addresseList = addressBiz.searchAddressFee(address,loginUser);
        if(addresseList != null && addresseList.size() > 0) {
            for(int i = 0; i < addresseList.size(); i ++) {
                Address a2 = addresseList.get(i);
                AddressFactory af = new AddressFactory();
                af.setAddressId(a2.getId());
                a2.setAddressFactoryList(addressFactoryBiz.list(af, loginUser));
            }
            return JsonResult.ok().setData(addresseList).put("type","address");
        } else {
            MaintainFee maintainFee = new MaintainFee();
            maintainFee.setProvince(address.getCustomerName());
            maintainFee.setDock(address.getDock());
            return JsonResult.ok().setData(maintainFeeBiz.searchAddressFee(maintainFee,loginUser)).put("type","maintainFee");
        }
    }
    /*public JsonResult searchAddressFee(Address address, User loginUser) {
        if(Utils.isEmpty(address.getDock())){
            return JsonResult.error("请选择码头!");
        }
        List<Address> addressList = addressBiz.searchAddressFee(address,loginUser);
        if(Utils.isNotEmpty(addressList)) {
            for(int i = 0; i < addressList.size(); i ++) {
                Address a2 = addressList.get(i);
                AddressFactory af = new AddressFactory();
                af.setAddressId(a2.getId());
                a2.setAddressFactoryList(addressFactoryBiz.list(af, loginUser));
            }
        }
        MaintainFee maintainFee = new MaintainFee();
        maintainFee.setProvince(address.getCustomerName());
        maintainFee.setDock(address.getDock());
        List<Address> addressList2 = maintainFeeBiz.searchAddressFee(maintainFee,loginUser);
        addressList.addAll(addressList2);
        return JsonResult.ok().setData(addressList);
    }*/

    @Override
    public JsonResult searchAddressFee2(Address address, User loginUser) {
        if(Utils.isEmpty(address.getDock()) || Utils.isEmpty(address.getCustomerName())){
            return JsonResult.error("请填写查询字段!");
        }
        List<AddressTemp> addresseList = addressBiz.searchCustomerAddress(address,loginUser);
        if(addresseList != null && addresseList.size() > 0) {
            return JsonResult.ok().setData(addresseList);
         } else {
            MaintainFee maintainFee = new MaintainFee();
            maintainFee.setProvince(address.getCustomerName());
            maintainFee.setDock(address.getDock());
            return JsonResult.ok().setData(maintainFeeBiz.searchCustomerAddress(maintainFee,loginUser));
        }
    }

    @Override
    public List<Address> dockList(Address address, User loginUser) {
        return addressBiz.dockList(address,loginUser);
    }

    @Override
    public Address getById(Address address) {
        return addressBiz.getById(address);
    }

    @Transactional
    @Override
    public JsonResult save(Address address, User loginUser) {
        if(StringUtil.isBlank(address.getCustomerId())) {
            return JsonResult.error("请填写完整的内容,操作失败!");
        }
        int key= addressBiz.save(address, loginUser);
        if(address.getAddressFactoryList()!=null && address.getAddressFactoryList().size()>0) {
            for (AddressFactory addressFactory : address.getAddressFactoryList()) {
                addressFactory.setAddressId(key + "");
                addressFactoryBiz.save(addressFactory, loginUser);
            }
        }
        return JsonResult.ok("添加成功");
    }

    @Transactional
    @Override
    public JsonResult update(Address address, User loginUser) {
        if(StringUtil.isBlank(address.getId())) {
            return JsonResult.error("请填写完整的内容,操作失败!");
        }
        addressBiz.update(address, loginUser);
        if(address.getAddressFactoryList()!=null && address.getAddressFactoryList().size()>0){
            for (AddressFactory addressFactory : address.getAddressFactoryList()) {
                addressFactory.setAddressId(address.getId());
                addressFactoryBiz.update(addressFactory, loginUser);
            }
        }
        return JsonResult.ok("修改成功");
    }
    @Transactional
    @Override
    public JsonResult customerUpdate(Address address, User loginUser) {
        if(StringUtil.isBlank(address.getId())) {
            return JsonResult.error("请填写完整的内容,操作失败!");
        }
        addressBiz.customerUpdate(address, loginUser);
        if(address.getAddressFactoryList()!=null && address.getAddressFactoryList().size()>0){
            for (AddressFactory addressFactory : address.getAddressFactoryList()) {
                addressFactory.setAddressId(address.getId());
                addressFactoryBiz.update(addressFactory, loginUser);
            }
        }
        return JsonResult.ok("修改成功");
    }

    @Override
    public JsonResult updatePrice(Address address, User loginUser) {
        if(StringUtil.isBlank(address.getId())) {
            return JsonResult.error("请填写完整的内容,操作失败!");
        }
        if (addressBiz.updatePrice(address, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }
    @Override
    public JsonResult updateState(Address address, User loginUser) {
        if(StringUtil.isBlank(address.getId())) {
            return JsonResult.error("请填写完整的内容,操作失败!");
        }
        address.setState("已通过");
        if (addressBiz.updateState(address, loginUser)) {
            return JsonResult.ok("审核通过");
        }
        return JsonResult.error("审核失败");
    }

    @Override
    public JsonResult remove(Address address, User loginUser) {
        if (addressBiz.remove(address, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
